//
// Created by jiangpenghui on 2023/4/18.
//

#ifndef CMAKECXX_SINGLETON_H
#define CMAKECXX_SINGLETON_H

#include <iostream>

using namespace std;

class Singleton {
    static Singleton s;
    int i;

    // 私有构造
    Singleton(int x) : i(x) {
        cout << "construction Singleton run " << endl;
    }

    // & 引用操作符私有化
    Singleton &operator=(Singleton &);

    Singleton(const Singleton &);

public:
    static Singleton &instance() { return s; }

    int getValue() { return i; }

    void setValue(int x) { i = x; }
};

// 静态成员s赋值
Singleton Singleton::s(43);


namespace tem {
    template<class T>
    class Singleton {
        Singleton(const Singleton &);

        Singleton &operator=(const Singleton &);

    protected:
        Singleton() {}

        virtual ~Singleton() {}

    public:
        static T &instance() {
            static T theInstance;
            return theInstance;
        }
    };
}
#endif //CMAKECXX_SINGLETON_H
